<?php
class Application_Model_address extends Application_Model_DomainObject{
	
        protected $_id;//
        protected $_state;//所在国家
        protected $_province;
        protected $_city;
        protected $_street;
        protected $_comment;
        protected $_postcode;
        protected $_linkMan;
        protected $_mobile;
        protected $_phone;
        protected $_userId;
        protected $_default;//默认值

        public static function   getbyuid($uid){        
            return Application_Model_address::getByUserId($uid);
        }
        
        /**
         * 返回用户的所有地址
         * @param type $userId
         * @return type 
         */
        public static function getByUserId($userId){
            return Application_Model_address::getDao()->queryAll(array('userId'=>$userId),'id desc');
        }


        public function getStateText(){
            if ($this->State)
                return Application_Model_city::load($this->State)->Name;
            return '';
        }
        
        private $_cityBy;
        
        /**
         * 
         * @return Application_Model_city
         */
        public function getCityBy(){
            if($this->_cityBy == NULL){
                $this->_cityBy = Application_Model_city::load($this->_state);
            }
            return $this->_cityBy;
        }
        /**
         * 获取列表
         * @author wuzw
         * @param string $username 昵称
         * @param int $count 页行数
         * @param int $offset 页
         * @return array object 列表对象
         */
        public static function getByFilter($quick, $count, $offset)
        {
            $where = array();
            $and = "";
            if($quick != NULL)
            {
                $uwhere[" nikename like "] = "%".$quick."%";
                $and = " or ";
                $uwhere[$and." storeno = "] = $quick;
                $userItem = Application_Model_User::getDao()->queryAll($uwhere);
                
                if($userItem){
                    $userid = "(";
                    foreach ($userItem as $uservalue) {
                        $userid .= $uservalue->id.",";
                    }
                    $userid .= "0)";
                    $where[" userId in "] = $userid;
                }
                
                $cwhere[" name like "] = "%".$quick."%";
                $and = " or ";
                $cwhere[$and." word = "] = $quick;
                $cityItem = Application_Model_city::getDao()->queryAll($cwhere);

                if($cityItem){
                    $state = "(";
                    foreach ($cityItem as $cityvalue) {
                        $state .= $cityvalue->id.",";
                    }
                    $state .= "0)";
                    $where[$and." state in "] = $state;
                }
            }       
            return Application_Model_address::getDao()->queryPage($where, " id desc ", $count, $offset);
        }
        /**
         * 根据userid，加载user对象
         * @author wuzw
         * @return type
         */
        public function getuserinfo()
        {
            if($this->_userId)
            {
                return Application_Model_User::load($this->_userId);
            }
        }
        public function  getStateName()
        {
            if($this->_state)
                return Application_Model_city::load ($this->_state);
        }
}